//
//  ProductListViewController.m
//  PrintPort
//
//  Created by hy002 on 2018/1/4.
//  Copyright © 2018年 HUAYU. All rights reserved.
//

#import "ProductListViewController.h"
#import "ProductModel.h"
#import "ImagelistModel.h"
#import "ProdTypeModel.h"
#import "LiveGoodsDetailViewController.h"
#import "ChildrenModel.h"
#import "QueryModel.h"
#import "BrandModel.h"
#import "BrowseTimeModel.h"
#import "NameIncludeModel.h"
#import "OriginPlaceModel.h"
#import "UnitModel.h"

static NSString *cellPath = @"LiveGoodsCell";
static NSString *cellSortPath = @"LivesortCell";
static NSString *cellscreenPath = @"screenCell";
static NSString * CellIdentifier = @"LivecollectCell";
@interface ProductListViewController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource,UITextFieldDelegate,UIScrollViewDelegate>
{
    UIButton *sortBtn;
    UIButton *salesBtn;
    UIView *backView;
    UIView *screeningView;
    UITextField *minField;
    UITextField *maxField;

}
@property (nonatomic, strong)UITableView *tableview;
@property (nonatomic, strong)UITableView *sortTableview;
@property (nonatomic, strong)UITableView *screeningTableview;
@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong)NSMutableArray *allGoodsAry;
@property (nonatomic, assign)NSInteger page;
@property (nonatomic, assign)NSInteger paixuType;
@property (nonatomic, strong)NSString *proID;
@property (nonatomic, assign)NSInteger closeType;
@property (nonatomic, strong)NSArray *leftAry;
@property (nonatomic, strong)NSArray *rightAry;
@property (nonatomic, strong)QueryModel *queryModel;
@property (nonatomic, strong)NSArray *allQueryAry;
@property (nonatomic, assign)NSInteger childType;
@property (nonatomic, assign)NSInteger brandType;
@property (nonatomic, assign)NSInteger nameType;
@property (nonatomic, assign)NSInteger placeType;
@property (nonatomic, assign)NSInteger unitType;
@property (nonatomic, assign)NSInteger browseType;
@property (nonatomic, strong)NSArray *allQueryTypeAry;
@property (nonatomic, strong)NSString *minStr;
@property (nonatomic, strong)NSString *maxStr;
@property (nonatomic, assign)BOOL isReloadParamer;;

@end

@implementation ProductListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _page = 0;
    _paixuType = 0;
    _proID = @"";
    _leftAry = @[@"综合排序",@"销量优先",@"价格由高到低",@"价格由低到高"];
    _allQueryAry = @[@"分类",@"品牌",@"价格",@"名称含有",@"商品产地",@"商品单位",@"浏览次数"];
    _allGoodsAry = [NSMutableArray arrayWithCapacity:0];
    [self reseTheState];
    [self creatUI];
    [self requestLifeGoodsList:@""];
    [self requestCommonQueryData];
}

- (void)reseTheState
{
    _isReloadParamer = NO;
    _minStr = @"";
    _maxStr = @"";
    ChildrenModel *model = _allTypeAry[0];
    _childType = [model.ID integerValue];
    _brandType = 0;
    _nameType = 0;
    _placeType = 0;
    _unitType = 0;
    _browseType = 0;
    _allQueryTypeAry = @[s_Integer(_childType),s_Integer(_brandType),@"",s_Integer(_nameType),s_Integer(_placeType),s_Integer(_unitType),s_Integer(_browseType)];

}

- (void)creatUI
{
    self.navigationItem.title = @"商品列表";
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = BacksColor;
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30*displaywidth)];
    topView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:topView];
    CGFloat W = (SCREEN_WIDTH-30*displaywidth)/2;
    NSArray *titAry = @[@"综合排序",@"筛选"];
    NSArray *imgAry = @[@"diankai",@"shaixuan"];
    for (int i = 0; i < titAry.count; i++)
    {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.backgroundColor =[UIColor clearColor];
        button.frame = CGRectMake(W*i, 0, W, 30*displaywidth);
        [button setImage:[UIImage imageNamed:imgAry[i]] forState:UIControlStateNormal];
        button.titleLabel.font = NormalFontWithSize(14.0+fontScale);
        UIColor *colors = ThemeColor;
        if (i != 0)
        {
            colors = BlaTiColor;
        }
        [button setTitleColor:colors forState:UIControlStateNormal];
        [button setTitle:titAry[i] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(closeBtnAct:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = 10+i;
        CGSize titleSize = [button.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:button.titleLabel.font}];
        CGSize imgSize = button.currentImage.size;
        UIEdgeInsets   titleEdgeInsets = UIEdgeInsetsMake(0, -imgSize.width-5, 0, imgSize.width+5);
        UIEdgeInsets imgEdgeInsets = UIEdgeInsetsMake(0, 5+titleSize.width, 0, -titleSize.width-5);
        button.titleEdgeInsets = titleEdgeInsets;
        button.imageEdgeInsets = imgEdgeInsets;
        button.selected = NO;
        [topView addSubview:button];
        if (i == 0)
        {
            sortBtn = button;
        }
        else
        {
            salesBtn = button;
        }
    }
    
    
    UIButton *changeBtn = [ZCControl createButtonWithFrame:CGRectMake(2*W, 0, 30*displaywidth, 30*displaywidth) ImageName:@"henglie" Target:self Action:@selector(changeBtnAction:) Title:nil Font:14.0];
    changeBtn.backgroundColor =[UIColor clearColor];
    changeBtn.selected = NO;
    [topView addSubview:changeBtn];
    UIView *lineAview = [[UIView alloc] initWithFrame:CGRectMake(W, 0, 1, 30*displaywidth)];
    lineAview.backgroundColor = LinesColor;
    [topView addSubview:lineAview];
    UIView *lineBview = [[UIView alloc] initWithFrame:CGRectMake(2*W, 0, 1, 30*displaywidth)];
    lineBview.backgroundColor = LinesColor;
    [topView addSubview:lineBview];
    UIView *lineCview = [[UIView alloc] initWithFrame:CGRectMake(0, 29*displaywidth, SCREEN_WIDTH, 1*displaywidth)];
    lineCview.backgroundColor = LinesColor;
    [topView addSubview:lineCview];
    
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 30*displaywidth, SCREEN_WIDTH, SCREEN_HEIGHT-STA_NAV_HEIGHT-30*displaywidth) style:UITableViewStylePlain];
    _tableview.backgroundColor = BacksColor;
    _tableview.tag = 11;
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableview];
    self.tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewHeaderData)];
    self.tableview.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreFootData)];
    self.tableview.mj_footer.automaticallyHidden = YES;
    self.tableview.estimatedRowHeight = 0;
    self.tableview.estimatedSectionHeaderHeight = 0;
    self.tableview.estimatedSectionFooterHeight = 0;
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 30*displaywidth, SCREEN_WIDTH, SCREEN_HEIGHT-STA_NAV_HEIGHT-30*displaywidth) collectionViewLayout:layout];
    _collectionView.backgroundColor = BacksColor;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CellIdentifier];
    _collectionView.hidden = YES;
    [self.view addSubview:_collectionView];
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewHeaderData)];
    self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreFootData)];
    
    backView = [[UIView alloc] initWithFrame:CGRectMake(0, 30*displaywidth, SCREEN_WIDTH, SCREEN_HEIGHT-STA_NAV_HEIGHT-30*displaywidth)];
    backView.backgroundColor = RGBA(0, 0, 0, 0.6);
    [self.view addSubview:backView];
    screeningView = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT-STA_NAV_HEIGHT)];
    screeningView.backgroundColor = RGBA(0, 0, 0, 0.6);
    [self.view addSubview:screeningView];
    self.sortTableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 120*displaywidth) style:UITableViewStylePlain];
    _sortTableview.backgroundColor = [UIColor whiteColor];
    _sortTableview.delegate = self;
    _sortTableview.dataSource = self;
    _sortTableview.tag = 22;
    _sortTableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [backView addSubview:_sortTableview];
    backView.hidden = YES;
    self.screeningTableview = [[UITableView alloc] initWithFrame:CGRectMake(75*displaywidth, 0, SCREEN_WIDTH-75*displaywidth, SCREEN_HEIGHT-STA_NAV_HEIGHT-SCREER_HeiX-40*displaywidth) style:UITableViewStylePlain];
    _screeningTableview.backgroundColor = [UIColor whiteColor];
    _screeningTableview.delegate = self;
    _screeningTableview.dataSource = self;
    _screeningTableview.tag = 33;
    _screeningTableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [screeningView addSubview:_screeningTableview];
    UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-STA_NAV_HEIGHT-SCREER_HeiX-40*displaywidth, SCREEN_WIDTH, 40*displaywidth+SCREER_HeiX)];
    whiteView.backgroundColor = [UIColor whiteColor];
    [screeningView addSubview:whiteView];
    UIButton *reloadBtn = [ZCControl createButtonWithFrame:CGRectMake(0 , 0, SCREEN_WIDTH/2, 40*displaywidth) ImageName:nil Target:self Action:@selector(reloadBtnAction) Title:@"重置" Font:15.0];
    [reloadBtn setTitleColor:ThemeColor forState:UIControlStateNormal];
    reloadBtn.backgroundColor = [UIColor whiteColor];
    [whiteView addSubview:reloadBtn];
    UIButton *completeBtn = [ZCControl createButtonWithFrame:CGRectMake(SCREEN_WIDTH/2 , 0, SCREEN_WIDTH/2, 40*displaywidth) ImageName:nil Target:self Action:@selector(completeAction) Title:@"完成" Font:15.0];
    [completeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    completeBtn.backgroundColor = ThemeColor;
    [whiteView addSubview:completeBtn];
    screeningView.hidden = YES;
    
}

//重置
- (void)reloadBtnAction
{
    [self reseTheState];
    [_screeningTableview reloadData];
}

//完成
- (void)completeAction
{
    salesBtn.selected = NO;
    [UIView animateWithDuration:0.5 animations:^{
        screeningView.frame = CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT-STA_NAV_HEIGHT);
    }];
    [self loadNewHeaderData];
}

- (void)loadNewHeaderData
{
    _page = 0;
    NSString *sort = @"";
    if (_paixuType == 1)
    {
        sort = @"productSales,desc";
    }
    if (_paixuType == 2)
    {
        sort = @"price,desc";
    }
    else if (_paixuType == 3)
    {
        sort = @"price,asc";
    }
    [self requestLifeGoodsList:sort];
}

- (void)loadMoreFootData
{
    _page ++;
    NSString *sort = @"";
    if (_paixuType == 1)
    {
        sort = @"productSales,desc";
    }
    if (_paixuType == 2)
    {
        sort = @"price,desc";
    }
    else if (_paixuType == 3)
    {
        sort = @"price.asc";
    }
    [self requestLifeGoodsList:sort];
}

//切换
- (void)changeBtnAction:(UIButton *)btn
{
    btn.selected = !btn.selected;
    if (btn.selected)
    {
        _tableview.hidden = YES;
        _collectionView.hidden = NO;
    }
    else
    {
        _tableview.hidden = NO;
        _collectionView.hidden = YES;
    }
}


- (void)closeBtnAct:(UIButton *)btn
{
    btn.selected = !btn.selected;
    if (btn.tag == 10)
    {
        salesBtn.selected = NO;
        screeningView.hidden = YES;
        NSString *imgStr = @"diankai";
        NSString *titStr = _leftAry[_paixuType];
        if (btn.selected)
        {
            imgStr = @"guanbiclose";
        }
        _sortTableview.frame = CGRectMake(0, 0, SCREEN_WIDTH, 120*displaywidth);
        [self changeBtnTitleAndImage:btn Imgstr:imgStr TitStr:titStr];
        backView.hidden = !btn.selected;
        [_sortTableview reloadData];
    }
    else
    {
        sortBtn.selected = NO;
        backView.hidden = YES;
//        screeningView.hidden = !btn.selected;
        screeningView.hidden = NO;
        [UIView animateWithDuration:0.5 animations:^{
            screeningView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-STA_NAV_HEIGHT);
        }];
        NSString *titStr = _leftAry[_paixuType];
        [self changeBtnTitleAndImage:sortBtn Imgstr:@"diankai" TitStr:titStr];
        [_screeningTableview reloadData];
    }
    
}

- (void)changeBtnTitleAndImage:(UIButton *)btn Imgstr:(NSString *)imgStr TitStr:(NSString *)titStr
{
    [btn setImage:IMAGE(imgStr) forState:UIControlStateNormal];
    [btn setTitle:titStr forState:UIControlStateNormal];
    CGSize titleSize = [btn.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:btn.titleLabel.font}];
    CGSize imgSize = btn.currentImage.size;
    UIEdgeInsets   titleEdgeInsets = UIEdgeInsetsMake(0, -imgSize.width-5, 0, imgSize.width+5);
    UIEdgeInsets imgEdgeInsets = UIEdgeInsetsMake(0, 5+titleSize.width, 0, -titleSize.width-5);
    btn.titleEdgeInsets = titleEdgeInsets;
    btn.imageEdgeInsets = imgEdgeInsets;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView.tag == 33)
    {
        return _allQueryAry.count;
    }
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView.tag == 22)
    {
        return _leftAry.count;
    }
    if (tableView.tag == 33)
    {
        return 1;
    }
    return _allGoodsAry.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell)
    {
        NSString *cellStr = cellPath;
        if (tableView.tag == 22)
        {
            cellStr = cellSortPath;
        }
        if (tableView.tag == 33)
        {
            cellStr = cellscreenPath;
        }
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellStr];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (tableView.tag == 11)
    {
        if (_allGoodsAry.count > 0)
        {
            ProductModel *productModel = _allGoodsAry[indexPath.row];
            UIImageView *imgView = [ZCControl createImageViewWithFrame:CGRectMake(15*displaywidth, 10*displaywidth, 80*displaywidth, 80*displaywidth) ImageName:@""];
            [imgView sd_setImageWithURL:[NSURL URLWithString:productModel.logoImgUrl]];
            imgView.contentMode = UIViewContentModeScaleAspectFill;
            imgView.layer.masksToBounds = YES;
            [cell.contentView addSubview:imgView];
            UILabel *titlab = [ZCControl createLabelWithFrame:CGRectMake(115*displaywidth, 5*displaywidth, SCREEN_WIDTH-130*displaywidth, 35*displaywidth) Font:12.0 Text:productModel.productName Color:RGB(77, 77, 77)];
            titlab.lineBreakMode = NSLineBreakByTruncatingTail;
            titlab.adjustsFontSizeToFitWidth = NO;
            [cell.contentView addSubview:titlab];
//            UIButton *priceBtn = [ZCControl createButtonWithFrame:CGRectMake(115*displaywidth, 70*displaywidth, 100*displaywidth, 20*displaywidth) ImageName:@"jifen" Target:nil Action:nil Title:string(@" ", productModel.price) Font:14.0];
//            priceBtn.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
//            priceBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
//            priceBtn.backgroundColor = [UIColor clearColor];
//            [priceBtn setTitleColor:priceColor forState:UIControlStateNormal];
//            [cell.contentView addSubview:priceBtn];
            
            UILabel *moneyLab = [ZCControl createLabelWithFrame:CGRectMake(115*displaywidth, 70*displaywidth, 100*displaywidth, 20*displaywidth) Font:12.0 Text:[NSString stringWithFormat:@"%@",productModel.priceDescribe] Color:RGB(241, 90, 36)];
            CGRect abounds = [moneyLab.text boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-200*displaywidth, 20*displaywidth) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:NormalFontWithSize(12.0+fontScale)} context:nil];
            CGFloat width1 = abounds.size.width+20;
            moneyLab.frame = CGRectMake(115*displaywidth, 70*displaywidth, width1, 20*displaywidth);
            [cell.contentView addSubview:moneyLab];
            CGFloat width2 = 0;
            if (!_isTH)
            {
                moneyLab.text = [NSString stringWithFormat:@"%@ +",productModel.priceDescribe];
                UILabel *integralLab = [ZCControl createLabelWithFrame:CGRectMake(width1+120*displaywidth, 70*displaywidth, 120*displaywidth, 20*displaywidth) Font:11.0 Text:[NSString stringWithFormat:@"%@积分",productModel.integral] Color:[UIColor whiteColor]];
                integralLab.textAlignment = NSTextAlignmentCenter;
                integralLab.layer.cornerRadius = 2;
                integralLab.layer.masksToBounds = YES;
                integralLab.backgroundColor = RGB(24, 159, 186);
                [cell.contentView addSubview:integralLab];
                CGRect bounds1 = [integralLab.text boundingRectWithSize:CGSizeMake(120*displaywidth, 20*displaywidth) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:NormalFontWithSize(11.0+fontScale)} context:nil];
                width2 = bounds1.size.width+10;
                if (bounds1.size.width > 120*displaywidth)
                {
                    width2 = 120*displaywidth;
                }
                integralLab.frame = CGRectMake(width1+120*displaywidth, 70*displaywidth, width2, 20*displaywidth);
            }
           
            UILabel *numLab = [ZCControl createLabelWithFrame:CGRectMake(width1+width2+140*displaywidth, 70*displaywidth, 100*displaywidth, 20*displaywidth) Font:12.0 Text:[NSString stringWithFormat:@" %@购买",productModel.productSales] Color:RGB(179, 179, 179)];
            [cell.contentView addSubview:numLab];
            
            UIView *lineFView = [[UIView alloc] initWithFrame:CGRectMake(0, 99*displaywidth, SCREEN_WIDTH, 1*displaywidth)];
            lineFView.backgroundColor = LinesColor;
            [cell.contentView addSubview:lineFView];
            
            
        }
    }
    else if(tableView.tag == 22)
    {
        NSString *nameStr = _leftAry[indexPath.row];
        UIColor *colors = BlaTiColor;
        BOOL hide = YES;
        if (_paixuType == indexPath.row)
        {
            colors = ThemeColor;
            hide = NO;
        }
        cell.textLabel.text = nameStr;
        cell.textLabel.textColor = colors;
        cell.textLabel.font = NormalFontWithSize(14.0+fontScale);
        UIImageView *closeImgview = [ZCControl createImageViewWithFrame:CGRectMake(SCREEN_WIDTH-50*displaywidth, 5*displaywidth, 20*displaywidth, 20*displaywidth) ImageName:@"gouprodu"];
        closeImgview.contentMode = UIViewContentModeScaleAspectFit;
        closeImgview.hidden = hide;
        [cell.contentView addSubview:closeImgview];
        UIView *lineBview = [[UIView alloc] initWithFrame:CGRectMake(0, 29*displaywidth, SCREEN_WIDTH, 1*displaywidth)];
        lineBview.backgroundColor = LinesColor;
        [cell.contentView addSubview:lineBview];
        
    }
    else
    {
        if (indexPath.section != 0)
        {
            UIView *lineAview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-95*displaywidth, 1*displaywidth)];
            lineAview.backgroundColor = LinesColor;
            [cell.contentView addSubview:lineAview];
        }
        UILabel *titLab = [ZCControl createLabelWithFrame:CGRectMake(10*displaywidth, 1*displaywidth, 150*displaywidth, 28*displaywidth) Font:14.0 Text:_allQueryAry[indexPath.section] Color:BlaTiColor];
        [cell.contentView addSubview:titLab];
        BOOL hidden = YES;
        if (indexPath.section == 2)
        {
            UIView *grayView = [[UIView alloc] initWithFrame:CGRectMake(10*displaywidth, 30*displaywidth, SCREEN_WIDTH-95*displaywidth, 35*displaywidth)];
            grayView.backgroundColor = LinesColor;
            grayView.layer.cornerRadius = 5;
            grayView.layer.masksToBounds = YES;
            [cell.contentView addSubview:grayView];
            NSArray *pladerAry = @[@"最低价",@"最高价"];
            NSArray *textAry = @[_minStr,_maxStr];
            for (int i = 0; i < pladerAry.count; i++)
            {
                CGFloat X = 5*displaywidth;
                if (i != 0)
                {
                    X = (SCREEN_WIDTH-95*displaywidth)/2+20*displaywidth;
                }
                UITextField *textfield = [ZCControl createTextFieldWithFrame:CGRectMake(X, 5*displaywidth, (SCREEN_WIDTH-95*displaywidth)/2-25*displaywidth, 25*displaywidth) placeholder:pladerAry[i] passWord:NO leftImageView:nil rightImageView:nil Font:12.0];
                textfield.textColor = BlaTiColor;
                textfield.textAlignment = NSTextAlignmentCenter;
                textfield.keyboardType = UIKeyboardTypeDecimalPad;
                textfield.text = textAry[i];
                textfield.delegate = self;
                textfield.backgroundColor = [UIColor whiteColor];
                [grayView addSubview:textfield];
                if (i == 0)
                {
                    minField = textfield;
                }
                else
                {
                    maxField = textfield;
                }

            }
            UILabel *toLab = [ZCControl createLabelWithFrame:CGRectMake((SCREEN_WIDTH-95*displaywidth)/2-20*displaywidth, 5*displaywidth, 40*displaywidth, 25*displaywidth) Font:18.0 Text:@"-" Color:[UIColor blackColor]];
            toLab.textAlignment = NSTextAlignmentCenter;
            [grayView addSubview:toLab];
            
            
        }
        else
        {
            if (_queryModel)
            {
                NSMutableArray *titStrAry = [NSMutableArray arrayWithCapacity:0];
                NSMutableArray *idStrAry = [NSMutableArray arrayWithCapacity:0];
                switch (indexPath.section)
                {
                    case 0:
                        for (ChildrenModel *model in _allTypeAry)
                        {
                            [titStrAry addObject:model.typeName];
                            [idStrAry addObject:s_str(model.ID)];
                        }
                        break;
                    case 1:
                        for (BrandModel *model in _queryModel.brand)
                        {
                            [titStrAry addObject:model.brandName];
                            [idStrAry addObject:s_str(model.ID)];
                        }
                        break;
                    case 3:
                        for (NameIncludeModel *model in _queryModel.nameInclude)
                        {
                            [titStrAry addObject:model.nameInclude];
                            [idStrAry addObject:s_str(model.ID)];
                        }
                        break;
                    case 4:
                        for (OriginPlaceModel *model in _queryModel.originPlace)
                        {
                            [titStrAry addObject:model.placeName];
                            [idStrAry addObject:s_str(model.ID)];
                        }
                        break;
                    case 5:
                        for (UnitModel *model in _queryModel.unit)
                        {
                            [titStrAry addObject:model.unitName];
                            [idStrAry addObject:s_str(model.ID)];
                        }
                        break;
                    case 6:
                        for (BrowseTimeModel *model in _queryModel.browseTime)
                        {
                            [titStrAry addObject:model.browseTime];
                            [idStrAry addObject:s_str(model.ID)];
                        }
                        break;
                        
                    default:
                        break;
                }
                if (titStrAry.count > 0)
                {
                    hidden = NO;
                    CGFloat W = (SCREEN_WIDTH-115*displaywidth)/3;
                    NSString *idStr = _allQueryTypeAry[indexPath.section];
                    if (!_isReloadParamer && indexPath.section == 0)
                    {
                        idStr = @"aaa";
                    }
                    for (int i = 0; i < titStrAry.count; i++)
                    {
                        UIButton *btn = [ZCControl createButtonWithFrame:CGRectMake(10*displaywidth + (W+10*displaywidth)*(i%3), 30*displaywidth+45*displaywidth*(i/3), W, 35*displaywidth) ImageName:nil Target:self Action:@selector(closeQueryAction:) Title:titStrAry[i] Font:12.0];
                        btn.titleLabel.lineBreakMode = 0;
                        btn.titleLabel.adjustsFontSizeToFitWidth = YES;
                        btn.tag = (indexPath.section+1)*1000+i;
                        UIColor *backColors = LinesColor;
                        UIColor *titColors = BlaTiColor;
                        if ([idStrAry[i] isEqualToString:idStr])
                        {
                            backColors = ThemeColor;
                            titColors = [UIColor whiteColor];
                        }
                        [btn setTitleColor:titColors forState:UIControlStateNormal];
                        btn.backgroundColor = backColors;
                        btn.layer.cornerRadius = 5;
                        btn.layer.masksToBounds = YES;
                        [cell.contentView addSubview:btn];

                    }
                }
            }
            
        }
        
        
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 22)
    {
        _paixuType = indexPath.row;
        [self closeBtnAct:sortBtn];
        [self loadNewHeaderData];
        [tableView reloadData];
    }
    else if (tableView.tag == 11)
    {
        ProductModel *productModel = _allGoodsAry[indexPath.row];
        LiveGoodsDetailViewController *vc = [[LiveGoodsDetailViewController alloc] init];
        vc.idStr = s_str(productModel.ID);
        if (!_isTH)
        {
            vc.isProbject = YES;
        }
        vc.isTeHui = _isTH;
        [self.navigationController pushViewController:vc animated:YES];
    }
    else
    {
        
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 22)
    {
        return 30*displaywidth;
    }
    if (tableView.tag == 33)
    {
        if (indexPath.section == 2)
        {
            return 75*displaywidth;
        }
        else
        {
            NSArray *numAry;
            switch (indexPath.section)
            {
                case 0:
                    numAry = _allTypeAry;
                    break;
                case 1:
                    numAry = _queryModel.brand;
                    break;
                case 3:
                    numAry = _queryModel.nameInclude;
                    break;
                case 4:
                    numAry = _queryModel.originPlace;
                    break;
                case 5:
                    numAry = _queryModel.unit;
                    break;
                case 6:
                    numAry = _queryModel.browseTime;
                    break;
                    
                default:
                    break;
            }
            NSInteger num = numAry.count/3;
            if (numAry.count %3 != 0)
            {
                num += 1;
            }
            return 30*displaywidth + 45*displaywidth*num;
        }
    }
    return 100*displaywidth;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _allGoodsAry.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    for (id subView in cell.contentView.subviews) {
        [subView removeFromSuperview];
    }
    CGFloat W = SCREEN_WIDTH/2;
    CGFloat X = 0;
    if (indexPath.row %2 == 1)
    {
        X = 7.5*displaywidth;
    }
    if (_allGoodsAry.count > 0)
    {
        cell.backgroundColor = BacksColor;
        ProductModel *productModel = _allGoodsAry[indexPath.row];
//        NSArray *imgArray = productModel.productImgList;
        UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(X, 0, W-7.5*displaywidth, 240*displaywidth)];
        whiteView.backgroundColor = [UIColor whiteColor];
        [cell.contentView addSubview:whiteView];
        UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, W-7.5*displaywidth, 170*displaywidth)];
//        if (imgArray.count > 0)
//        {
//            ImagelistModel *imgModel = imgArray[0];
        [imageView1 sd_setImageWithURL:[NSURL URLWithString:productModel.logoImgUrl]];
//        }
        imageView1.contentMode = UIViewContentModeScaleAspectFill;
        imageView1.layer.masksToBounds = YES;
        [whiteView addSubview:imageView1];
        UILabel *titlab = [ZCControl createLabelWithFrame:CGRectMake(15*displaywidth, 170*displaywidth, W-37.5*displaywidth, 25*displaywidth) Font:12.0 Text:productModel.productName Color:RGB(77, 77, 77)];
        titlab.lineBreakMode = NSLineBreakByTruncatingTail;
        titlab.adjustsFontSizeToFitWidth = NO;
        [cell.contentView addSubview:titlab];
        //        UILabel *moneyLab = [ZCControl createLabelWithFrame:CGRectMake(15*displaywidth, 210*displaywidth, 100*displaywidth, 20*displaywidth) Font:12.0 Text:[NSString stringWithFormat:@"￥%@",productModel.price] Color:priceColor];
        //        [cell.contentView addSubview:moneyLab];
//        UIButton *priceBtn = [ZCControl createButtonWithFrame:CGRectMake(15*displaywidth, 210*displaywidth, 100*displaywidth, 20*displaywidth) ImageName:@"jifen" Target:nil Action:nil Title:string(@" ", productModel.price) Font:14.0];
//        priceBtn.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
//        priceBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
//        priceBtn.backgroundColor = [UIColor clearColor];
//        [priceBtn setTitleColor:priceColor forState:UIControlStateNormal];
//        [cell.contentView addSubview:priceBtn];
       
        UILabel *priceLab = [ZCControl createLabelWithFrame:CGRectMake(15*displaywidth, 217.5*displaywidth, W-37.5*displaywidth, 15*displaywidth) Font:12.0 Text:[NSString stringWithFormat:@"￥%.2lf元",[productModel.price floatValue]] Color:priceColor];
        [cell.contentView addSubview:priceLab];
        if (!_isTH)
        {
            priceLab.text = [NSString stringWithFormat:@"￥%.2lf元 +",[productModel.price floatValue]];
            priceLab.frame = CGRectMake(15*displaywidth, 200*displaywidth, W-37.5*displaywidth, 15*displaywidth);
            UILabel *integralLab = [ZCControl createLabelWithFrame:CGRectMake(15*displaywidth, 217.5*displaywidth, (W-37.5*displaywidth)/2, 15*displaywidth) Font:11.0 Text:[NSString stringWithFormat:@"%@积分",productModel.integral] Color:[UIColor whiteColor]];
            integralLab.textAlignment = NSTextAlignmentCenter;
            integralLab.layer.cornerRadius = 2;
            integralLab.layer.masksToBounds = YES;
            integralLab.backgroundColor = RGB(24, 159, 186);
            [cell.contentView addSubview:integralLab];
            CGRect bounds1 = [integralLab.text boundingRectWithSize:CGSizeMake(W-37.5*displaywidth, 15*displaywidth) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:NormalFontWithSize(11.0+fontScale)} context:nil];
            CGFloat width1 = bounds1.size.width+10;
            if (bounds1.size.width > (W-37.5*displaywidth)/2)
            {
                width1 = (W-37.5*displaywidth)/2;
            }
            integralLab.frame = CGRectMake(15*displaywidth, 217.5*displaywidth, width1, 15*displaywidth);
        }
        
        UILabel *numLab = [ZCControl createLabelWithFrame:CGRectMake(100*displaywidth, 215*displaywidth, 80*displaywidth, 20*displaywidth) Font:12.0 Text:[NSString stringWithFormat:@" %@购买",productModel.productSales] Color:RGB(179, 179, 179)];
        [cell.contentView addSubview:numLab];
        
        
    }
    
    
    return cell;
}

//定义每一个cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(SCREEN_WIDTH/2, 245*displaywidth);
}

//cell的点击事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //cell被电击后移动的动画
    [collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionTop];
    ProductModel *productModel = _allGoodsAry[indexPath.row];
    LiveGoodsDetailViewController *vc = [[LiveGoodsDetailViewController alloc] init];
    vc.idStr = s_str(productModel.ID);
    [self.navigationController pushViewController:vc animated:YES];
    
}

// 将点击tableviewcell的时候收回 searchBar 键盘
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 33)
    {
        [tableView endEditing:YES];
    }
    return indexPath;
}
// 滑动的时候 searchBar 回收键盘
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [minField resignFirstResponder];
    [maxField resignFirstResponder];
}

- (void)closeQueryAction:(UIButton *)btn
{
    NSInteger section = btn.tag/1000-1;
    NSInteger index = btn.tag%1000;
    switch (section)
    {
        case 0:
        {
            ChildrenModel *model = _allTypeAry[index];
            _childType = Integer_str(model.ID);
            _isReloadParamer = YES;
            break;
        }
        case 1:
        {
            BrandModel *model = _queryModel.brand[index];
            _brandType = Integer_str(model.ID);
            break;
        }
        case 3:
        {
            NameIncludeModel *model = _queryModel.nameInclude[index];
            _nameType = Integer_str(model.ID);
            break;
        }
        case 4:
        {
            OriginPlaceModel *model = _queryModel.originPlace[index];
            _placeType = Integer_str(model.ID);
            break;
        }
        case 5:
        {
            UnitModel *model = _queryModel.unit[index];
            _unitType = Integer_str(model.ID);
            break;
        }
        case 6:
        {
            BrowseTimeModel *model = _queryModel.browseTime[index];
            _browseType = Integer_str(model.ID);
            break;
        }
            
        default:
            break;
    }
    _allQueryTypeAry = @[s_Integer(_childType),s_Integer(_brandType),@"",s_Integer(_nameType),s_Integer(_placeType),s_Integer(_unitType),s_Integer(_browseType)];
    [_screeningTableview reloadData];
    
}



- (void)requestLifeGoodsList:(NSString *)sortKey
{
    WEAKSELF;
    if ([_maxStr floatValue] < [_minStr floatValue] && [_maxStr floatValue] > 0)
    {
        [LTools showMBProgressWithText:@"最高价必须大于最低价" addToView:self.view];
        return;
    }
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    [dic setObject:_modelID forKey:@"productTypeParentId"];
    if (_isReloadParamer)
    {
        [dic setObject:s_Integer(_childType) forKey:@"productTypeId"];

    }
    [dic setObject:s_Integer(_page) forKey:@"page"];
    if (![sortKey isEqualToString:@""])
    {
        [dic setObject:sortKey forKey:@"sort"];
    }
    if (_brandType > 0)
    {
        [dic setObject:s_Integer(_brandType) forKey:@"brandId"];
    }
    if ([_minStr floatValue] > 0)
    {
        [dic setObject:_minStr forKey:@"priceMinQuery"];
    }
    if ([_maxStr floatValue] > 0)
    {
        [dic setObject:_maxStr forKey:@"priceMaxQuery"];
    }
    if (_nameType > 0)
    {
        [dic setObject:s_Integer(_nameType) forKey:@"nameInclude"];
    }
    if (_placeType > 0)
    {
        [dic setObject:s_Integer(_placeType) forKey:@"originPlaceId"];
    }
    if (_unitType > 0)
    {
        [dic setObject:s_Integer(_unitType) forKey:@"unitId"];
    }
    if (_browseType > 0)
    {
        [dic setObject:s_Integer(_browseType) forKey:@"browseTimeQuery"];
    }
    [HYBaseHandler requestWithTheFormUrl:SERVER_LifeGoodsList Parmers:dic Success:^(id obj) {
        [weakSelf.tableview.mj_header endRefreshing];
        [weakSelf.tableview.mj_footer endRefreshing];
        [weakSelf.collectionView.mj_header endRefreshing];
        [weakSelf.collectionView.mj_footer endRefreshing];
        if ([obj server_Getcodecheck])
        {
            NSArray *goodsAry = [ProductModel mj_objectArrayWithKeyValuesArray:obj[@"data"][@"content"]];
            dispatch_async(dispatch_get_main_queue(), ^{
                if (weakSelf.page == 0)
                {
                    [weakSelf.allGoodsAry removeAllObjects];
                }
                [weakSelf.allGoodsAry addObjectsFromArray:goodsAry];
                [weakSelf.tableview reloadData];
                [weakSelf.collectionView reloadData];

            });
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
        [weakSelf.tableview.mj_header endRefreshing];
        [weakSelf.tableview.mj_footer endRefreshing];
        [weakSelf.collectionView.mj_header endRefreshing];
        [weakSelf.collectionView.mj_header endRefreshing];
        
    }];
}

//
- (void)requestCommonQueryData
{
    WEAKSELF;
    [HYBaseHandler requestWithTheFormUrl:SERVER_queryData Parmers:nil Success:^(id obj) {
        [weakSelf.collectionView.mj_header endRefreshing];
        if ([obj server_Getcodecheck])
        {
            weakSelf.queryModel = [QueryModel mj_objectWithKeyValues:obj[@"data"]];
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.screeningTableview reloadData];
            });
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
        [weakSelf.collectionView.mj_header endRefreshing];
        
    }];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if (textField == minField)
    {
        _minStr = textField.text;
    }
    else if (textField == maxField)
    {
        _maxStr = textField.text;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
